//
//  NSDecimalNumber+Extension.m
//  RuiHeAppApp
//
//  Created by Yan Chen on 2024/5/16.
//

#import "NSDecimalNumber+Extension.h"

@implementation NSDecimalNumber (Extension)
+ (NSString*)decimalPlacesWithoutRoundDownWithDigit:(int)digit value:(double)value
{
    // 创建NSNumberFormatter对象
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;//数值超过1000会带,号
    // 设置小数位数为2
    [numberFormatter setMinimumFractionDigits:digit];
    [numberFormatter setMaximumFractionDigits:digit];
    [numberFormatter setMinimumIntegerDigits:1];
    numberFormatter.roundingMode = NSNumberFormatterRoundDown;
    // 将数字格式化为字符串
    NSString *formattedNumber = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]];
    NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:formattedNumber];

    return [NSString stringWithFormat:@"%@",decNumber];
}
///保留n位小数,四舍五入
+ (NSString*)decimalPlacesWithRoundUpWithDigit:(int)digit value:(double)value
{
    // 创建NSNumberFormatter对象
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
//    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;//数值超过1000会带,号
    // 设置小数位数为2
    [numberFormatter setMinimumFractionDigits:digit];
    [numberFormatter setMaximumFractionDigits:digit];
    [numberFormatter setMinimumIntegerDigits:1];
    numberFormatter.roundingMode = NSNumberFormatterRoundHalfUp;
    // 将数字格式化为字符串
    NSString *formattedNumber = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:value]];
    NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:formattedNumber];
    
    return [NSString stringWithFormat:@"%@",decNumber];
}

+ (NSDecimalNumber*)decimalNumberWithDouble:(double)value
{
    NSNumber *num = [NSNumber numberWithDouble:value];
    if (num==nil) {
        return [NSDecimalNumber decimalNumberWithString:@"0"];
    }
    NSString *valueStr = [NSString stringWithFormat:@"%lf",value];
    return [NSDecimalNumber decimalNumberWithString:valueStr];
}

+ (NSString*)decimalStringWithDouble:(double)value
{
    NSNumber *num = [NSNumber numberWithDouble:value];
    if (num==nil) {
        return @"0";
    }
   NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithDouble:value];
   
    return [NSString stringWithFormat:@"%@",decimal];
}

+ (NSString*)decimalStringWithDouble:(double)value nullStr:(NSString*)placeHoldStr
{
    NSNumber *num = [NSNumber numberWithDouble:value];
    if (num==nil) {
        return placeHoldStr;
    }
   NSDecimalNumber *decimal = [NSDecimalNumber decimalNumberWithDouble:value];
   
    return [NSString stringWithFormat:@"%@",decimal];
}

+ (NSString*)decimalStringWithDoubleString:(NSString*)value nullStr:(NSString*)placeHoldStr
{
    if (ValidNullData(value).length == 0) {
        return placeHoldStr;
    }
    //-8.437时出现了精度问题,增加下面两行代码解决. 参考:https://www.jianshu.com/p/c05166937d98
    double num = [value doubleValue];
    NSString *dstr = [NSString stringWithFormat:@"%f",num];
    NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:dstr];
    return [NSString stringWithFormat:@"%@",number];
}
@end
